package com.archtecture.model.patterns;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Toast;

import com.archtecture.model.exceptions.NegocioException;
import com.spgf.model.entities.utils.ModelAb;
import com.spgf.model.utils.AndroidUtil;

public abstract class PadraoActivityMnt<Model extends ModelAb> extends PadraoActivity<Model> implements OnItemLongClickListener {

	public void atualizarLista() throws NegocioException {}

	public void configurarExcluir(Integer pCodigo) {

		try {
			getRN().excluir(pCodigo);
			atualizarLista();
			Toast.makeText(this, "Registro excluido com sucesso!", Toast.LENGTH_LONG).show();
		} catch (NegocioException e) {
			Toast.makeText(this, AndroidUtil.obterMensagemErro(e), Toast.LENGTH_SHORT).show();
		}
	}

	protected boolean executarLongClick(AdapterView<?> pParent, View pView, int pPosition, long pID) throws NegocioException {
		return false;
	}

	@SuppressWarnings("rawtypes")
	public abstract Class getControleCadastro();

	@Override
	public boolean onItemLongClick(AdapterView<?> pParent, View pView, int pPosition, long pID) {
		try {
			return executarLongClick(pParent, pView, pPosition, pID);
		} catch (final NegocioException e) {
			Toast.makeText(pView.getContext(), e.getMensagemErro().toString(), Toast.LENGTH_SHORT).show();
			return false;
		}
	}

	@Override
	protected void onRestart() {
		super.onRestart();

		try {
			atualizarLista();
		} catch (NegocioException e) {
			Toast.makeText(this, AndroidUtil.obterMensagemErro(e), Toast.LENGTH_SHORT).show();
		}
	}

}
